<template>
	<view class="home">
		<scroll-view scroll-x="true" class="navscroll">
			<view class="item" :class="index==navIndex?'active':''" v-for="(item,index) in navlist"
				@click="clickNav(index,item.id)" :key="item.id">
				{{item.classname}}
			</view>
		</scroll-view>
		<view class="content">
			<view class="row" v-for="item in newslist" :key="item.id">
				<newsbox :item="item"></newsbox>
			</view>
		</view>
		<view class="nodata" v-if="!newslist.length">
			<image src="../../static/images/nodata.png" mode="widthFix"></image>
		</view>
		<view class="buttom" v-if="newslist.length">
			<view class="loading" v-if="loading==1">新闻加载中...</view>
			<view class="nomaore" v-else="loading==2">-没有更多了-</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: 'Hello',
				navIndex: 0,
				navlist: [],
				newslist: [],
				currentPage: 1,
				currentNavId: 50,
				loading: 0 // 0默认 1加载中 2没有更多
			}
		},
		onLoad() {
			this.getNavata();
			this.getNewsData();
		},
		onReachBottom() {
			this.currentPage++;
			if (this.loading != 2) {
				this.loading = 1;
			}
			this.getNewsData();
		},
		methods: {
			// 点击顶部导航
			clickNav(index, cid) {
				this.navIndex = index;
				this.currentNavId = cid;
				this.currentPage = 1;
				this.loading = 0;
				this.newslist = [];
				this.getNewsData();
			},
			// 获取首页顶部栏信息
			getNavata() {
				uni.request({
					url: "https://ku.qingnian8.com/dataApi/news/navlist.php",
					success: res => {
						// console.log(res);
						this.navlist = res.data;
					}
				})
			},
			// 获取新闻列表
			getNewsData() {
				uni.request({
					url: "https://ku.qingnian8.com/dataApi/news/newslist.php",
					data: {
						cid: this.currentNavId,
						page: this.currentPage,
					},
					success: res => {
						console.log(res);
						if (res.data.length == 0) {
							this.loading = 2;
						}
						this.newslist = [...this.newslist, ...res.data];
					}
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.navscroll {
		height: 100rpx;
		background-color: #f7f8fa;
		white-space: nowrap;
		position: fixed;
		top: var(--window-top);
		left: 0;
		z-index: 10;

		/deep/ ::-webkit-scrollbar {
			width: 4px !important;
			height: 1px !important;
			overflow: auto !important;
			background: transparent !important;
			-webkit-appearance: auto !important;
			display: block;
		}

		.item {
			font-size: 40rpx;
			display: inline-block;
			line-height: 100rpx;
			padding: 0 30rpx;
			color: #333;

			&.active {
				color: #31c27c;
			}
		}


	}

	.content {
		padding: 30rpx;
		padding-top: 130rpx;

		.row {
			border: 1rpx dashed #efefef;
			padding: 20rpx 0;
		}
	}

	.nodata {
		display: flex;
		justify-content: center;

		image {
			width: 500rpx;
		}
	}

	.buttom {
		text-align: center;
		font-size: 26rpx;
		color: #888;
		line-height: 2em;
	}
</style>